package com.asistencia.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asistencia.businness.AssistanceBusiness;
import com.asistencia.businness.CareerBusiness;
import com.asistencia.businness.CourseBusinness;
import com.asistencia.businness.EnrollmentBusiness;
import com.asistencia.businness.ProfessorBusiness;
import com.asistencia.businness.ProfessorCourseBusiness;
import com.asistencia.businness.ScheduleBusiness;
import com.asistencia.businness.SemesterBusiness;
import com.asistencia.businness.StudentBusiness;
import com.asistencia.businness.TypeCourseBusinness;
import com.asistencia.domain.Assistance;
import com.asistencia.domain.Course;
import com.asistencia.domain.Enrollment;
import com.asistencia.domain.Professor;
import com.asistencia.domain.ProfessorCourse;
import com.asistencia.domain.Semester;
import com.asistencia.domain.Student;
import com.asistencia.domain.TypeCourse;
import com.asistencia.security.UserSecutiry;
import com.asistencia.util.Constant;

@Controller
public class IndexController {

        @Autowired
        private StudentBusiness studentBusiness;
        @Autowired
        private ProfessorBusiness professorBusiness;
        @Autowired
        private AssistanceBusiness assitanceBusiness;
        @Autowired
        private SemesterBusiness semesterBusiness;

        @Autowired
        private ProfessorCourseBusiness professorcourseBusiness;
        @Autowired
        private CourseBusinness courseBusinness;
        @Autowired
        private TypeCourseBusinness typeCourseBusinness;
        @Autowired
        private CareerBusiness careerBusiness;
        @Autowired
        private ScheduleBusiness scheduleBusiness;

        @Autowired
        private EnrollmentBusiness enrollmentBusinnes;

        @RequestMapping(value = "/index.tecsup", method = RequestMethod.GET)
        public String goHome(HttpSession session, Student student, Professor professor,
                        ModelMap modelMap) {

                UserSecutiry userSecutiry = (UserSecutiry) SecurityContextHolder.getContext()
                                .getAuthentication().getPrincipal();

                // Declaramos variables a usar para el alumno
                Semester semester;
                List<Semester> lstSemesters;
                List<Enrollment> lstEnrollments;
                List<ProfessorCourse> lstProfessorCourses;
                        
                lstSemesters = semesterBusiness.listAll();
                
                semester = lstSemesters.get(lstSemesters.size() - 1);
                
                if (isNumeric(userSecutiry.getUsername())) {
                        student = studentBusiness.getById(userSecutiry.getIdUser());
                        session.setAttribute(Constant.USER_IN_SESSION, student);

                        lstEnrollments = enrollmentBusinnes.listByStudentSemester(student.getIdStudent(), semester.getIdSemester());

                        // Recorremos todas las matriculas obtenidas para inyectar sus
                        // depencias
                        for (Enrollment enrollment : lstEnrollments) {
                                enrollment.setMyStudent(student);
                                enrollment.setMySemester(semester);

                                Course course = courseBusinness.getCourseById(enrollment.getMyCourse().getIdCourse());
                                // obtenemos el tipo del curso
                                TypeCourse typeCourse = typeCourseBusinness.getById(course.getMyTypeCourse().getIdTypeCourse());
                                course.setMyTypeCourse(typeCourse);

                                List<Assistance> lstAssistance = assitanceBusiness.lisByEnrollment(enrollment.getIdEnrollment());

                                enrollment.setMyCourse(course);
                                enrollment.setMyAssistance(lstAssistance);
                        }

                        modelMap.addAttribute("lstEnrollments", lstEnrollments);

                        return "student-index";

                } else {
                        professor = professorBusiness.getById(userSecutiry.getIdUser());
                        session.setAttribute(Constant.USER_IN_SESSION, professor);
                        
                        //Listamos los semestres habilitados para el profesor
                        lstSemesters.clear();
                        lstProfessorCourses = professorcourseBusiness.listSemestersByIdProfesor(professor.getIdProfessor());
                        if(lstProfessorCourses != null){                        	
                        	for (ProfessorCourse professorCourse : lstProfessorCourses) {
                        		Semester temp = semesterBusiness.getById(professorCourse.getMySemester().getIdSemester());
                        		lstSemesters.add(temp);
                        	}
                        }

                        lstProfessorCourses = professorcourseBusiness.listProfessorCourseByProfessorSemester(professor.getIdProfessor(), semester.getIdSemester());
                        
                        if(lstProfessorCourses != null){
                        	for (ProfessorCourse professorCourse : lstProfessorCourses) {
                                professorCourse.setMyprofessor(professor);
                                professorCourse.setMySemester(semester);
                                
                                Course course = courseBusinness.getCourseById(professorCourse.getMyCourse().getIdCourse());
                                // obtenemos el tipo del curso
                                TypeCourse typeCourse = typeCourseBusinness.getById(course.getMyTypeCourse().getIdTypeCourse());
                                course.setMyTypeCourse(typeCourse);
                                
                                professorCourse.setMyCourse(course);
                        	}
                        }
                        
                        modelMap.addAttribute("semester", semester);
                        modelMap.addAttribute("lstSemesters", lstSemesters);
                        modelMap.addAttribute("lstProfessorCourses", lstProfessorCourses);
                        
                        return "professor-index";
                }
        }

        private boolean isNumeric(String secuence) {
                try {
                        Integer.parseInt(secuence);
                        return true;
                }
                catch (Exception e) {
                        return false;
                }
        }

        @RequestMapping(value = "/reporte-diario.tecsup", method = RequestMethod.GET)
        public String reporte() {
                return "reporte-diario";
        }

        @RequestMapping(value = "/horarios.tecsup", method = RequestMethod.GET)
        public String horarios() {
                return "horarios";
        }

}
